// userPackage/pages/healthRecords/healthRecords.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    userId: '',

    questionList: []

  },

  selectHandle(e) {
    console.log(e);
    var qindex = e.currentTarget.dataset.qindex,
      aindex = e.currentTarget.dataset.aindex,
      questionList = this.data.questionList;

    questionList[qindex].a.forEach((ele, index) => {
      ele.isSelect = index == aindex
    })


    this.setData({
      questionList
    })


  },

  getData() {
    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + "scSystem/listHealth",
      that = this,
      questionList = this.data.questionList;

    app.wxRequest(url, {}, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;
          console.log(datas);
          wx.hideLoading()


          datas.forEach(ele => {
            var arr = ele.healthAnswer.split('@'),
              list = [];
            arr.forEach(item => {
              list.push({
                text: item,
                isSelect: false
              })
            })
            questionList.push({
              healthId: ele.healthId,
              q: ele.healthQuestion,
              a: list
            })
          })


          console.log(questionList);


          that.setData({
            questionList

          })

          that.getUserAnswer()
          wx.stopPullDownRefresh()
        }
      },
      err => {
        console.log(err);
      })
  },

  saveHandle() {

    var questionList = this.data.questionList,
      str = '';

    questionList.forEach((ele, index) => {
      if (ele.a.every(item => {
          return item.isSelect == false
        })) {
        str += (index + 1) + '、'
      }
    })



    if (str != '') {
      str = str.slice(0, -1)
      wx.showToast({
        title: '第 ' + str + ' 题未选择',
        icon: 'none'
      })
      return
    }

    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + "scSystem/addHealthUser",
      that = this,
      questionList = this.data.questionList,
      obj = {
        userId: that.data.userId,
        healthIds: '',
        answerIds: ''
      };

    questionList.forEach(ele => {
      var anindex = -1;
      ele.a.forEach((item, iIndex) => {
        if (item.isSelect) {
          anindex = iIndex
        }
      })
      obj.healthIds += ele.healthId + ','
      obj.answerIds += anindex + ','
    })
    console.log(obj);

    app.wxRequest(url, obj, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;
          console.log(datas);
          wx.hideLoading()
          wx.stopPullDownRefresh()
        }
      },
      err => {
        console.log(err);
      })
  },

  // 获取用户的答案
  getUserAnswer() {
    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + "scSystem/listHealthUser",
      that = this,
      questionList = this.data.questionList;

    app.wxRequest(url, {
        userId: that.data.userId
      }, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;

          datas.forEach(ele => {
            questionList.forEach(item => {
              if (item.healthId == ele.healthId) {
                item.a[ele.userAnswer].isSelect = true
              }
            })
          })
          that.setData({
            questionList
          })
          wx.hideLoading()
          wx.stopPullDownRefresh()
        }
      },
      err => {
        console.log(err);
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log(options);

    this.setData({
      userId: options.userId
    })

    this.getData()

    console.log(this.data);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})